/*
 * AppExecptionHandler.cpp
 *
 *  Created on: 2018年11月13日
 *      Author: zhengboyuan
 */

#include "AppExecptionHandler.h"

#define XCRASHREPORT_CRASH_REPORT_APP	TEXT("CrashReport.bat")

AppExecptionHandler::AppExecptionHandler()
{
}

AppExecptionHandler::~AppExecptionHandler()
{
}

void AppExecptionHandler::setup()
{
	SetUnhandledExceptionFilter(AppUnhandledExceptionFilter);
}


LONG WINAPI AppExecptionHandler::AppUnhandledExceptionFilter(struct _EXCEPTION_POINTERS* ExceptionInfo)
{
	run(XCRASHREPORT_CRASH_REPORT_APP);
	return EXCEPTION_EXECUTE_HANDLER;
}

bool AppExecptionHandler::run(const char* filepath)
{
	TCHAR szCommandLine[MAX_PATH];
	lstrcpy(szCommandLine, filepath);

	STARTUPINFO si;
	ZeroMemory(&si, sizeof(si));
	si.cb = sizeof(si);
	si.dwFlags = STARTF_USESHOWWINDOW;
	si.wShowWindow = SW_HIDE;   // 隐藏窗口

	PROCESS_INFORMATION pi;
	ZeroMemory(&pi, sizeof(pi));

	if (CreateProcess(
		NULL,					// name of executable module
		szCommandLine,			// command line string
		NULL,					// process attributes
		NULL,					// thread attributes
		FALSE,					// handle inheritance option
		0,						// creation flags
		NULL,					// new environment block
		NULL,					// current directory name
		&si,					// startup information
		&pi))					// process information
	{
		// XCrashReport.exe was successfully started, so
		// suppress the standard crash dialog

		CloseHandle(pi.hProcess);
		CloseHandle(pi.hThread);
		return true;
	}
	else
	{
		// XCrashReport.exe was not started - let
		// the standard crash dialog appear
		return false;
	}
}

bool AppExecptionHandler::run()
{
    return run(XCRASHREPORT_CRASH_REPORT_APP);
}